package com.shengzhe.ostudent.utils;

import com.shengzhe.ostudent.entity.base.BaseEntity;
import org.apache.commons.lang.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import java.util.TimeZone;

/**
 * @describe 时间处理类
 * @author s.li
 * @date 2021/2/8 11:18
 */
public class LocalDateUtils extends DateFormatUtils {
    public static final String YYYY_MM_DD = BaseEntity.YYYY_MM_DD;
    public static final String YYYY_MM_DD_HH_MM_SS = BaseEntity.YYYY_MM_DD_HH_MM_SS;

    /**
     * 获取当天的开始时间
     * @return 开始时间
     */
    public static Date getNowStartTime(){
        return getNowStartTime(null);
    }

    /**
     * 获取当天的结束时间
     * @return 结束时间
     */
    public static Date getNowEndTime(){
        return getNowEndTime(null);
    }

    /**
     * 获取当天的开始时间
     * @param date 时间
     * @return 开始时间
     */
    public static Date getNowStartTime(Date date){
        Calendar calendar = Calendar.getInstance();
        if(!Objects.isNull(date)){
            calendar.setTime(date);
        }
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取当天的结束时间
     * @param date 时间
     * @return 结束时间
     */
    public static Date getNowEndTime(Date date){
        Calendar calendar = Calendar.getInstance();
        if(!Objects.isNull(date)){
            calendar.setTime(date);
        }
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    /**
     * 获得本周一0点时间
     * @return Date
     */
    public static Date getNowWeekStartTime() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }

    /**
     * 获得本周日最后点时间
     * @return Date
     */
    public static Date getNowWeekEndTime() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getNowWeekStartTime());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        cal.set(Calendar.HOUR, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    /**
     * 获得本月第一天0点时间
     * @return Date
     */
    public static Date getNowMonthStartTime() {
        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.MONTH, 0);
        // 设置为1号,当前日期既为本月第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获得本月最后一天结束时间
     * @return Date
     */
    public static Date getNowMonthEndTime() {
        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.MONTH, 0);
        // 获取当前月最后一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    /**
     * 获得月第一天0点时间
     * @return Date
     */
    public static Date getMonthStartTime(Date month) {
        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(month);
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.MONTH, 0);
        // 设置为1号,当前日期既为本月第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获得月最后一天结束时间
     * @return Date
     */
    public static Date getMonthEndTime(Date month) {
        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(month);
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.MONTH, 0);
        // 获取当前月最后一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    /**
     * 获取当前年的开始时间
     * @return Date
     */
    public static Date getCurrentYearStartTime() {
        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.DATE, 0);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取当前年的结束时间
     * @return Date
     */
    public static Date getCurrentYearEndTime() {
        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        int year = calendar.get(Calendar.YEAR);
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        return calendar.getTime();
    }
    /**
     * 根据传入参数、获取前/后几天0点时间
     * @return Date
     */
    public static Date getPassingStartTime(int passing) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar calendar=Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        calendar.add(Calendar.DATE,passing);
        Date time=calendar.getTime();
        String format = simpleDateFormat.format(time);
        try {
            Date date = simpleDateFormat.parse(format);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据传入参数、获取前/后几天 结束时间
     * @return Date
     */
    public static Date getPassingEndTime(int passing) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Calendar calendar=Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        calendar.add(Calendar.DATE,passing);
        Date time=calendar.getTime();
        String format = simpleDateFormat.format(time);
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

}
